package com.cloudcc.cloudframe.ui.viewgroup;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * Created by dev on 2017/7/5.
 */

public class VerticalScrollView extends ScrollView {

    private float xDistance, yDistance, xLast, yLast;

    public VerticalScrollView(Context context) {
        super(context);
    }

    public VerticalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VerticalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;

                if (xDistance > yDistance) {
                    return false;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }

    public boolean isScrolledToTop, isScrolledToBottom;

//    @Override
//    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
//        super.onScrollChanged(l, t, oldl, oldt);
//        if (android.os.Build.VERSION.SDK_INT < 9) {  // API 9及之后走onOverScrolled方法监听
//            if (getScrollY() == 0) {    // 小心踩坑1: 这里不能是getScrollY() <= 0
//                isScrolledToTop = true;
//                isScrolledToBottom = false;
//            } else if (getScrollY() + getHeight() - getPaddingTop() - getPaddingBottom() == getChildAt(0).getHeight()) {
//                // 小心踩坑2: 这里不能是 >=// 小心踩坑3（可能忽视的细节2）：这里最容易忽视的就是ScrollView上下的padding　
//                isScrolledToBottom = true;
//                isScrolledToTop = false;
//            } else {
//                isScrolledToTop = false;
//                isScrolledToBottom = false;
//            }
////            notifyScrollChangedListeners();
//        }
//    }

}